package com.youdoneed.news.news.web;

import com.youdoneed.base.common.JWTUtils;
import com.youdoneed.news.common.vo.ResultMessage;
import com.youdoneed.news.news.service.CmfNavService;
import com.youdoneed.news.news.vo.CmfNavVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@SuppressWarnings("unchecked")
@RestController
@RequestMapping(value = "/newsNav", method = {RequestMethod.GET, RequestMethod.POST})
@Api(value = "/newsNav", description = "新闻导航模块控制器")
public class CmfNavController {

    private static final Logger LOGGER = LoggerFactory.getLogger(CmfNavController.class);

    @Autowired
    private CmfNavService cmfNavService;

    /**
     * 获取用户关注的新闻导航列表
     *
     * @return
     * @author hexz
     * @date 2017.7.3
     */
    @RequestMapping(value = "/getFocusNewsNav", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "获得用户关注的新闻导航列表", value = "获得用户关注的新闻导航列表")
    public ResultMessage<List<CmfNavVo>> getAttentionTerms(
            @ApiParam(value = "客户端请求头附带参数") @RequestHeader(value = "Authorization") String token) {
        String userId = JWTUtils.getTokenUserId(token);
        LOGGER.info("开始查询关注的新闻导航列表" + userId);
        List<CmfNavVo> cmfNavVos = cmfNavService.getFocusNewsNavcation(userId);
        return ResultMessage.newSuccess("获取关注的导航成功").setData(cmfNavVos);
    }

    /**
     * 新闻频道编辑查询
     *
     * @author liudong
     * @date 2017.7.6
     */
    @RequestMapping(value = "/getEditNewNavData", method = {RequestMethod.POST})
    @ApiOperation(notes = "新闻频道编辑-查询其中data={'myNewsNav':[{CmfNav}],'rcdNewsNav':[{CmfNav}]}"
            , value = "新闻频道编辑-查询")
    public ResultMessage<Map<String, List<CmfNavVo>>> getEditNewNavData(
            @ApiParam(value = "客户端请求头附带参数") @RequestHeader(value = "Authorization") String token) {
        String userId = JWTUtils.getTokenUserId(token);
        Map<String, List<CmfNavVo>> navMap = cmfNavService.getEditNewNavData(userId);
        return ResultMessage.newSuccess("获取成功").setData(navMap);
    }

    /**
     * 新闻频道编辑更新
     *
     * @author liudong
     * @date 2017.7.6
     */
    @RequestMapping(value = "/updateEditNewNavData", method = {RequestMethod.POST})
    @ResponseBody
    @ApiOperation(notes = "新闻频道编辑-更新:上传参数为，data={},List<CmfNavVo> cmfNavList的json", value = "新闻频道编辑-更新")
    public ResultMessage<String> updateEditNewNavData(
            @ApiParam("List<CmfNavVo> cmfNavList的json") @RequestParam(required = false) String data,
            @ApiParam(value = "客户端请求头附带参数") @RequestHeader(value = "Authorization") String token) {
        String userId = JWTUtils.getTokenUserId(token);
        cmfNavService.editNewsNavData(userId, data);
        return new ResultMessage<>().toSuccess();
    }

    /**
     * 获取趣视频导航栏
     *
     * @return
     */
    @RequestMapping(value = "/getJokeVideoNav", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "获取趣视频导航栏", value = "获取趣视频导航栏")
    public ResultMessage<List<CmfNavVo>> getJokeVideoNav() {
        List<CmfNavVo> videoNavs = cmfNavService.getSubNewsNav(7);
        return ResultMessage.newSuccess("获取趣视频导航栏成功").setData(videoNavs);
    }
}
